package org.csz.service;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

@Service
@Slf4j
public class OrderService {

	public static final String ORDER_PREFIX = "order:";

	@Autowired
	private StringRedisTemplate redisTemplate;

	/**
	 * 生成订单号
	 */
	public void addOrder() {
		// random 是线程安全的但是效率不高
		int keyId = ThreadLocalRandom.current().nextInt(1000)+1;
		String serialNo = UUID.randomUUID().toString();

		String key = ORDER_PREFIX + keyId;
		String value = "J京东D"+serialNo;
		redisTemplate.opsForValue().set(key, value);
		log.info("添加订单成功，订单号：{}", keyId);
	}

	/**
	 * 获取订单
	 */
	public String getOrderById(String orderId) {
		String s = redisTemplate.opsForValue().get(ORDER_PREFIX + orderId);
		log.info("获取订单成功，订单号：{}", orderId);
		return s;
	}
}
